<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class km_kemasan extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('km_kemasan_model', 'Kemasan');
		$this->load->model('kst_combo_model', 'Combo');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Kemsan";
		
		// Define Combo Data
		$data['arr_application']	= $this->Combo->ITApplication();
		
		viewByTemplate('km_kemasan/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->Kemasan->GetJSON();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		// Validation
		//if (post("kemasan_nama") == '') {
			//$error->Add("kemasan_nama", "Application", $tpl_empty);
		//}
		
		// if (post("no_isi") == '') {
		// 	$error->Add("no_isi", "no isi", $tpl_empty);
		// } else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM km_kemasan WHERE no_isi = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("no_isi"))))."'")) > 0) {
		// 	$error->Add("no_isi", "no isi", $tpl_duplicate);
		// }

		//if (post("role_name") == '') {
			//$error->Add("role_name", "Role Name", $tpl_empty);
		//}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{

					$query = $this->db->query("SELECT no_isi FROM (SELECT * FROM km_kemasan	 ORDER BY no_isi DESC) WHERE rownum <=1");
	 					if ($query->row_array()) {
	 						$getquery = $query->row_array();
	 						$rowcount = $getquery['no_isi'];
	 						$autonum = $rowcount + 1;
	 					} else {
					$autonum = 1;
				}
				// Fill Post Data
				$data	= array('no_isi' =>$autonum,
								'isi' =>$this->input->post('isi')
						);
				
				if (post("mode") == "create")
				{
					
					$this->Kemasan->Add($data);
					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					
					$this->Kemasan->Update($data, array('no_isi'	=> $data['no_isi']));

					$ls_response = "Data Tersimpan [edited]";
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$no_isi	= get('no_isi');
		
		if ($no_isi != "")
		{
			// Delete Single
			$arr_rule	= array('no_isi' => $no_isi);
			$ls_result = $this->Kemasan->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('no_isi' => $row['no_isi']);
				$ls_result .= $this->Kemasan->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}
}
?>